<%@ page import="com.party.core.model.system.TargetType" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="memberForm" uri="http://www.tongxingzhe.cn/memberForm" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${project == null ? '新建' : '编辑'}相册 </title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/form.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_act.css">
    <style TYPE="text/css">
        .form-tip {
            float: left;
            margin-right: 10px;
            width: calc(100% - 110px);
        }

        .add-form-content .form-inner {
            padding: 10px;
        }

        .border {
            border: 1px solid #eee;
            padding-left: 0px;
            padding-right: 0px;
            border-radius: 4px;
        }

        fieldset legend {
            font-size: 18px;
            margin-left: 30px;
        }

        .p131 {
            padding-left: 196px;
        }
    </style>
</head>
<body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/photo/photoproject/save.do">
                    <input type="hidden" name="id" value="${project.id}">
                    <c:if test="${project != null || not empty project}">
                        <input type="hidden" name="circleId" value="${project.circleId}">
                    </c:if>

                    <div class="layui-form-item">
                        <label class="layui-form-label">所属合作商<span class="f-verify-red">*</span></label>
                        <div class="layui-input-inline">
                            <input type="hidden" name="memberId" lay-verify="memberId" value="${memberId}"/>
                            <input type="text" id="member_name"
                                   value="${memberForm:getRealNameById(memberId)}" class="layui-input"
                                   readonly/>
                        </div>
                        <c:if test="${empty project.id}">
                            <a class="layui-btn layui-btn-danger" type="button"
                               onclick="openSelMemberDialog(this,'选择所属合作商','${ctx}/system/member/selectPartner.do','950px','570px')">
                                选择
                            </a>
                        </c:if>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">相册名称<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block ">
                            <c:if test="${circle != null && not empty circle}">
                                <input type="text" name="name" lay-verify="name" placeholder="相册名称"
                                       class="layui-input form-tip" maxlength="30"
                                       value="${circle.name}" style="width: 50%"/>
                            </c:if>
                            <c:if test="${circle == null ||  empty circle}">
                                <input type="text" name="name" lay-verify="name" placeholder="相册名称"
                                       class="layui-input form-tip" maxlength="30"
                                       value="${project.name}" style="width: 50%"/>
                            </c:if>
                            <div class="layui-form-mid layui-word-aux">最多30字符</div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">相册简介</label>
                        <div class="layui-input-block ">
                                        <textarea name="introduction" class="layui-textarea calc-110"
                                                  lay-verify="introduction" maxlength="200"><c:if
                                                test="${circle != null && not empty circle}">${circle.memo}</c:if><c:if
                                                test="${circle == null &&  empty circle}">${project.introduction}</c:if></textarea>
                            <div class="layui-form-mid layui-word-aux">最多200字符</div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">相册海报<span class="f-verify-red">*</span></label>
                        <div class="cover-content">
                            <c:if test="${project == null || empty project.pic}">
                                <c:if test="${circle == null &&  empty circle}">
                                    <input type="hidden" name="pic" id="pic" lay-verify="pic"
                                           value=""/>
                                    <span id="cover-img" class="cover-img"
                                          style="background-image:url(${ctx}/image/posterImg.png)"></span>
                                </c:if>
                                <c:if test="${circle != null && not empty circle}">
                                    <input type="hidden" name="pic" id="pic" lay-verify="pic"
                                           value="${circle.logo}"/>
                                    <span id="cover-img" class="cover-img"
                                          style="background-image:url('${circle.logo}')"></span>
                                </c:if>
                            </c:if>
                            <c:if test="${project != null && not empty project.pic}">
                                <input type="hidden" name="pic" id="pic" lay-verify="pic"
                                       value="${project.pic}"/>
                                <span id="cover-img" class="cover-img"
                                      style="background-image:url('${project.pic}')"></span>
                            </c:if>
                            <a class="layui-btn layui-btn-danger" id="sel_pic_img">+添加图片</a>
                            <div class="form-word-aux">建议尺寸：800x450</div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">活动地点</label>
                        <div class="layui-input-block">
                            <input type="text" name="place" lay-verify="place" placeholder="活动地点"
                                   class="layui-input"
                                   maxlength="100"
                                   value="${project.place}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">开始日期</label>
                        <div class="layui-input-block">
                            <input class="layui-input" name="startDate" lay-verify="startDate"
                                   placeholder="开始时间"
                                   id="startDate" style="width: 190px; float: left; margin-right: 10px;" readonly
                                   value='<fmt:formatDate value="${project.startTime}" pattern="yyyy-MM-dd HH:mm" />'/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">结束日期</label>
                        <div class="layui-input-block">
                            <input class="layui-input" name="endDate" lay-verify="endDate"
                                   placeholder="结束时间"
                                   id="endDate" style="width: 190px; float: left; margin-right: 10px;" readonly
                                   value='<fmt:formatDate value="${project.endTime}" pattern="yyyy-MM-dd HH:mm" />'/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">操作权限<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                            <div class="layui-input-inline" style="width: auto">
                                <c:if test="${circleId != null && not empty circleId}">
                                    <c:if test="${project == null &&  empty project}">
                                        <input type="hidden" name="circleId" value="${circle.id}">
                                    </c:if>
                                    <input type="radio" name="operatePermission" value="0"
                                           lay-filter="isOperate"
                                           title="所有通讯录人员可操作" ${not empty project && project.operatePermission == 0 ? 'checked="checked"' : ''}>
                                    <input type="radio" name="operatePermission" value="1"
                                           lay-filter="isOperate"
                                           title="对通讯录每个会员类型单独设置操作权限" ${empty project || project.operatePermission == 1? 'checked="checked"' : ''}>
                                    <div class="pl30 p131" id="specialDiv" ${empty project || project.operatePermission == 1? '' : 'style="display: none;"'}  >
                                        <c:forEach var="tag" items="${ctList}" varStatus="status">
                                            <dl class="circleTag_${tag.id}" data-id="${tag.id}">
                                                <input type="hidden" name="circleTags[${status.index}].id"
                                                       value="${tag.id}"/>
                                                <input type="hidden"
                                                       name="circleTags[${status.index}].tagName"
                                                       value="${tag.tagName}"/>
                                                <dt class="red">${tag.tagName}</dt>
                                                <dd class="pl20">
                                                    <input type="radio"
                                                           name="circleTags[${status.index}].isOperate"
                                                           value="1"
                                                           title="可操作" ${empty tag.isOperate || tag.isOperate == 1 ? 'checked="checked"' : ''} />
                                                    <input type="radio"
                                                           name="circleTags[${status.index}].isOperate"
                                                           value="0"
                                                           title="不可操作" ${not empty tag.isOperate && tag.isOperate == 0 ? 'checked="checked"' : ''} />
                                                </dd>

                                            </dl>
                                        </c:forEach>
                                        <dl>
                                            <dt class="red">其他</dt>
                                            <dd class="pl20">
                                                <input type="radio"
                                                       name="noTypeIsOperate"
                                                       value="1"
                                                       title="可操作" ${empty noTypeIsOperate || noTypeIsOperate == 1 ? 'checked="checked"' : ''} />
                                                <input type="radio"
                                                       name="noTypeIsOperate"
                                                       value="0"
                                                       title="不可操作" ${not empty noTypeIsOperate && noTypeIsOperate == 0 ? 'checked="checked"' : ''} />
                                            </dd>
                                        </dl>
                                    </div>
                                </c:if>
                                <c:if test="${circleId == null ||  empty circleId}">
                                    <input type="radio" name="operatePermission" value="2"
                                           title="可操作" ${not empty project && project.operatePermission == 2 ? 'checked="checked"' : ''}>
                                    <input type="radio" name="operatePermission" value="3"
                                           title="不可操作" ${empty project || project.operatePermission == 3? 'checked="checked"' : ''}>
                                </c:if>
                            </div>
                        </div>
                    </div>
                    <c:if test="${not empty project.id}">
                        <div class="layui-form-item">
                            <div class="layui-inline" style="vertical-align: bottom;">
                                <label class="layui-form-label">分享编辑<span class="f-verify-red">*</span></label>
                                <div class="layui-input-inline">
                                    <input type="radio" name="editShare" lay-filter="editShare" value="0"
                                           title="否" ${project == null || empty project.editShare || project.editShare == 0 ? 'checked="checked"' : ''}>
                                    <input type="radio" name="editShare" lay-filter="editShare" value="1"
                                           title="是" ${project != null && project.editShare == 1 ? 'checked="checked"' : ''}>
                                </div>
                                <div class="layui-form-mid layui-word-aux">说明：第三方可以通过链接和访问编码在PC端操作相册，可以上传照片、添加分类、移动照片、设为精选，可以删除自己上传的照片。</div>
                            </div>
                        </div>
                        <div class="layui-form-item editShareDiv" ${project == null || empty project.editShare || project.editShare == 0 ? 'style="display:none"' : ''}>
                            <div class="layui-inline" style="margin-left: 130px;">
                                <div>
                                    <div class="dib">
                                        <span>访问编码</span>
                                        <input type="text" name="account" value="${thirdMember.account}"
                                               class="layui-input"
                                               style="display: inline-block; width: 120px" readonly/>
                                    </div>
                                    <div class="dib" style="width: 220px">
                                        <span>访问密码</span>
                                        <input type="text" name="password" value="${thirdMember.password}"
                                               class="layui-input" style="display: inline-block; width: 150px"
                                               readonly/>
                                    </div>
                                    <div class="mt10">
                                        <span>众筹编辑链接</span>
                                        <input type="text" name="url" value="${thirdMember.url}" class="layui-input"
                                               style="display: inline-block; width: 400px" readonly/>
                                        <input type="hidden" name="uri" value="${thirdMember.uri}"/>
                                        <input type="hidden" name="alias" value="${thirdMember.alias}"/>
                                        <div class="dib">
                                            <button id="copySign" class="layui-btn layui-btn-danger" type="button">复制链接、编码及密码</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </c:if>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit
                               lay-filter="myForm">立即提交</a>
                            <a href="${ctx}/photo/photoproject/list.do"
                               class="layui-btn layui-btn-primary">取消</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <!--底部-->
        <%@include file="../include/footer.jsp" %>
        <c:if test="${not empty memberId}">
            <c:set var="isExpire" value="${memberForm:isExpire(memberId, null)}"/>
        </c:if>
    </section>
</div>

<script type="text/javascript" src="${ctx}/script/common/form_cache.js"></script>
<script type="text/javascript" src="${ctx}/static/clipboard/clipboard.min.js"></script>
<script>
    var form, ue, formcache, laydate;
    txz.initHeader({
        nav: [{
            name: '相册管理',
            href: '${ctx}/photo/photoproject/list.do'
        }, {
            name: '${activity == null ? '发布' : '编辑'}相册',
            curr: true
        }],
        btns: [{
            type: 'back'
        }]
    });
    $(function () {

        layui.use(['form', 'laydate', 'element'], function () {
            form = layui.form, laydate = layui.laydate;
            var radioValue = null;
            var element = layui.element;
            form.on('radio(isFree)', function (data) {
                radioValue = data.value;
                if (radioValue == "noFree") {
                    $("[name=price]").prop("disabled", false);
                } else {
                    $("[name=price]").val("");
                    $("[name=price]").prop("disabled", true);
                }
            });

            form.on('radio(isOperate)', function (data) {
                var value = data.value;
                if (value == "0" || value == "2" || value == "3") {
                    $("#specialDiv").hide();
                } else if (value == "1") {
                    $("#specialDiv").show();
                }
            });

            form.on('radio(isOperate)', function (data) {
                var value = data.value;
                if (value == "0" || value == "2" || value == "3") {
                    $("#specialDiv").hide();
                } else if (value == "1") {
                    $("#specialDiv").show();
                }
            });

            form.on('radio(editShare)', function (data) {
                var editShare = $("[name=editShare]:checked").val();
                if (editShare == 1) {
                    $.post("${ctx}/photo/photoproject/generateInfo.do", {id: '${project.id}'}, function (res) {
                        if (res.success) {
                            $("[name=url]").val(location.origin + '/web' + res.data.url);
                            $("[name=uri]").val('/web' + res.data.url);
                            $("[name=alias]").val(res.data.alias);
                            $("[name=account]").val(res.data.account);
                            $("[name=password]").val(res.data.password);

                            $(".editShareDiv").show();
                        }
                    })
                } else {
                    $(".editShareDiv").hide();
                }
            })

            //监听提交
            form.on('submit(myForm)', function (data) {
                txz.submitObject(data.elem, function (callBack) {
                    var action = $("#myForm").attr("action");
                    $.post(action, $('#myForm').serialize(), function (res) {
                        typeof callBack === 'function' && callBack();
                        if (res.success) {
                            util.layerMsgSuccess("提交成功", function () {
                                location.href = "${ctx}/photo/photoproject/list.do";
                            });
                        } else {
                            util.layerMsgError("提交失败")
                        }
                    });
                })
                return false;
            });

            // 自定义校验
            form.verify({
                memberId: function (value) {
                    if (value == "") {
                        return "请选择合作商";
                    }
                    if ('${isExpire}' == 'true' && '${project.id}' == '') {
                        return "当前合作商已到期，不能发布数据"
                    }
                },
                name: function (value) {
                    if (value == "") {
                        return "请填写相册名称";
                    }
                    if (value.length > 30) {
                        return "相册名称最多30个字符";
                    }
                },
                pic: function (value) {
                    if (value == "") {
                        return "请选择相册海报";
                    }
                }
            });

            //日期
            var start = {
                elem: '#startDate',
                min: '2015-01-01 00:00:00',
                max: '2099-06-16 23:59',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm'
            };

            var end = {
                elem: '#endDate',
                min: '2015-01-01 00:00:00',
                max: '2099-06-16 23:59',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm' //日期格式
            };
            laydate.render(start);
            laydate.render(end);

        });
        txz.saveStatus = false;

        $('#sel_pic_img').click(function () {
            txz.openSelImg({
                min: 1,
                max: 1,
                info: '建议尺寸：800x450',
                type: '<%=TargetType.PHOTO_POSTER.getCode()%>',
                cb: function (imgs) {
                    $('#cover-img').css('background-image', 'url(' + imgs[0].path + ')');
                    $('#pic').val(imgs[0].path);
                }
            })
        })
        $('#copySign').on('click', function () {
            var cpval = "";
            cpval += "链接：" + $("[name=url]").val();
            cpval += "  访问编码：" + $("[name=account]").val();
            cpval += "  密码：" + $("[name=password]").val();

            $('#copySign').attr('data-clipboard-text', cpval);
            var clipboard = new ClipboardJS('#copySign');
            clipboard.on('success', function (e) {
                util.layerMsgSuccess("复制成功")
            });
            clipboard.on('error', function (e) {
                util.layerMsgError('复制失败');
            });
        })
    })


    //此方法用于加载缓存数据
    function loadCacheData(cacheData) {


        //加载封面图
        if (cacheData['pic']) {
            $('#cover-img').css('background-image', 'url(' + cacheData['pic'] + ')');
        }
        /*form.render();
         txz.Map.init({
         layForm: form,
         data: {
         cityId: cacheData.cityId,
         areaInput: cacheData.areaSelect || cacheData.areaInput,
         place: cacheData.place,
         lat: cacheData.lat,
         lng: cacheData.lng
         }
         });*/


        // 信息收集表单
        if (cacheData['formItemCache']) {
            var formItemCache = JSON.parse(cacheData['formItemCache']);
            if (formItemCache.length > 0) {
                loadFormItem(formItemCache);
            }
        }
    }

    function openSelMemberDialog(that, title, url, width, height) {
        txz.openDialogIframe({
            width: width,
            height: height,
            title: title,
            url: url,
            cb: function (opt) {
                var content = opt.content;
                if (content.doSubmit()) {
                    var data = content.returnData();
                    $('input[name=memberId]').val(data.id);
                    $('#member_name').val(data.name);
                    top.layer.close(opt.index);
                }
            }
        })
    }
</script>
</body>
</html>